///////////////////////////////////////////////////////////////////////////
// Copyright 2009 Arabic Game Developer Network, All rights reserved
// www.agd-online.com - info@agdn-online.com
//
// File: text.cpp
// Description: Simple text rendering utility.
//
///////////////////////////////////////////////////////////////////////////

// This file is part of kvalleyX.
// 
// kvalleyX is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// kvalleyX is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with kvalleyX.  If not, see <http://www.gnu.org/licenses/>.

/////////////////// #includes ///////////////////
#include "game.h"


/////////////////// #defines ///////////////////
#define TEXT_CHARACTER_WIDTH 16
#define TEXT_CHARACTER_HEIGHT 16
#define TEXT_CHARACTERS_IN_ROW 8


/////////////////// FontMonospace Class Implementation ///////////////////
bool FontMonospace::Load(const char *pszFontName)
{
	char szInFileName[255];
	sprintf(szInFileName,"%s.fnt",pszFontName);
	char szFileName[255];
	if (!GetGameFilePath(szFileName,szInFileName,GameDir_Font))
		return false;

	if (!CoreLib::FontLoad(szFileName))
		return false;

	return true;
}

/////////////////////////////////////////////////////////////////////////////////////////////
// Function Name : FontMonospace::Draw
//
// Purpose : Draws the specified text at the required position, starting from the bottom-left
//			 corner, and increasing to the right (only supports English, and not all characters).
//
// Params : 
//    int iScreenX : Screen pixel coordinate of text left.
//    int iScreenY : Screen pixel coordinate of text bottom.
//    const char *pszText : String to draw. No line-breaks allowed.
//
// Return Value : None.
/////////////////////////////////////////////////////////////////////////////////////////////
void FontMonospace::Draw(int iScreenX, int iScreenY, const char *pszText) const
{
	CoreLib::RECT rcSource;
	while (*pszText)
	{
		GetCharacterRect(*(pszText++),rcSource);
		CoreLib::FontPutChar(iScreenX,iScreenY,rcSource);
		iScreenX += TEXT_CHARACTER_WIDTH;
	}
}

/////////////////////////////////////////////////////////////////////////////////////////////
// Function Name : FontMonospace::GetCharacterRect
//
// Purpose : Calculates the source letter rectangle from the letters map.
//
// Params : 
//    char cChar : Character to look for.
//    RECT& rcChar : (Out) Source character rectangle.
//
// Return Value : None.
/////////////////////////////////////////////////////////////////////////////////////////////
void FontMonospace::GetCharacterRect(char cChar,CoreLib::RECT& rcChar) const
{
	int iIndex = -1;
	if ((cChar >= 'a') && (cChar <= 'z'))
		iIndex = cChar-'a';
	else if ((cChar >= 'A') && (cChar <= 'Z'))
		iIndex = cChar-'A';
	else if ((cChar >= '0') && (cChar <= '9'))
		iIndex = cChar-'0'+26;
	else switch (cChar)
	{
		case '.': iIndex = 36; break;
		case ':': iIndex = 37; break;
		case '!': iIndex = 38; break;
		case '?': iIndex = 39; break;
		case '-': iIndex = 40; break;
		case '[': iIndex = 41; break;
		case ']': iIndex = 42; break;
		case '@': iIndex = 43; break; // (R)
		case '*': iIndex = 44; break; // (C)
		case ' ': iIndex = 45; break;
		case '\'': iIndex = 46; break;
		case '|': iIndex = 47; break;
		case ',': iIndex = 48; break;
		case '(': iIndex = 49; break;
		case ')': iIndex = 50; break;
		case '>': iIndex = 51; break;
		case '_': iIndex = 52; break;
	}

	if (iIndex == -1)
		iIndex = 63; // Unknown character...

	int iRow = iIndex/TEXT_CHARACTERS_IN_ROW;
	int iCol = iIndex-iRow*TEXT_CHARACTERS_IN_ROW;
	rcChar.left = iCol*TEXT_CHARACTER_WIDTH;
	rcChar.top = iRow*TEXT_CHARACTER_HEIGHT;
	rcChar.right = rcChar.left + TEXT_CHARACTER_WIDTH;
	rcChar.bottom = rcChar.top + TEXT_CHARACTER_HEIGHT;
}

////////////////// End of File : text.cpp //////////////////